package com.ruikong.dazhi.service.impl.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.dao.base.PersonnelMapper;
import com.ruikong.dazhi.dao.base.RoleMapper;
import com.ruikong.dazhi.dao.base.RolePermissionMapper;
import com.ruikong.dazhi.entity.base.Role;
import com.ruikong.dazhi.model.input.BasePageInput;
import com.ruikong.dazhi.model.input.RoleInput;
import com.ruikong.dazhi.service.base.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @program: dailychemicalmes
 * @description:
 * @author: tybengine
 * @create: 2020-12-02 09:54
 **/
@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private PersonnelMapper personnelMapper;

    /**
     * 添加人员信息
     * @param result
     * @param roleInput
     * @return
     */
    @Transactional
    public int addRole(BaseResult result, RoleInput roleInput){
        if(roleInput.getRoleName().equals("")){
            result.setMsg(ResultApi.addFailMsg);
            return 0;
        }

        int count = roleMapper.getRoleCountByRoleName(roleInput.getRoleName());
        if(count>0){
            result.setMsg("该角色名称已存在");
            return 0;
        }
        Role role = new Role();
        role.setRoleName(roleInput.getRoleName());
        role.setComment(roleInput.getComment());
        int effectLine = roleMapper.addRole(role);
        if(effectLine > 0){
            Map<Integer,String> map = roleInput.getMaps();
            for (Integer item : map.keySet()) {
                int effectRp = rolePermissionMapper.addRolePermission(role.getRoleId(), item);
            }
            result.setMsg(ResultApi.addSuccessMsg);
            return 1;
        }else {
            result.setMsg(ResultApi.addFailMsg);
            return 0;
        }
    }

    /**
     * 获取列表
     *
     * @param input
     * @return
     */
    public PageInfo<Role> getRoleListPage(BasePageInput input){
        PageHelper.startPage(input.getPageNumber(), input.getPageSize());
        List<Role> roles = roleMapper.getRoleList(input.getSearchText());
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return pageInfo;
    }

    /**
     * 获取列表
     *
     * @return
     */
    public List<Role> getRoleList(){
        return roleMapper.getRoleList("");
    }


    /**
     * 编辑
     * @return
     */
    @Transactional
    public int updateRole(RoleInput roleInput, BaseResult result){
        if("".equals(roleInput.getRoleName())){
            result.setMsg(ResultApi.addFailMsg);
            return 0;
        }

        if(roleInput.getRoleId()==0){
            result.setMsg(ResultApi.addFailMsg);
            return 0;
        }

        Role getRole = roleMapper.getByRoleId(roleInput.getRoleId());
        if(getRole==null){
            result.setMsg(ResultApi.addFailMsg);
            return 0;
        }

        if(!getRole.getRoleName().equals(roleInput.getRoleName())){//名称和数据库中的不相同
            int count = roleMapper.getRoleCountByRoleName(roleInput.getRoleName());
            if(count>0){
                result.setMsg("该角色名称已存在");
                return 0;
            }
        }

        Role role = new Role();
        role.setRoleId(roleInput.getRoleId());
        role.setRoleName(roleInput.getRoleName());
        role.setComment(roleInput.getComment());
        int effect = roleMapper.updateRole(role);
        if(effect!=0){
            //删除角色下已有的所有权限
            rolePermissionMapper.deleteRolePermissionByRoleId(roleInput.getRoleId());

            //从新添加角色权限
            Map<Integer,String > map = roleInput.getMaps();
            for (Integer item : map.keySet()) {
                int effectRp = rolePermissionMapper.addRolePermission(role.getRoleId(), item);
            }
            result.setMsg(ResultApi.addSuccessMsg);
            return 1;
        }
        else {
            result.setMsg(ResultApi.addFailMsg);
            return 0;
        }
    }


    /**
     * 删除
     * @param role
     * @return
     */
    public int deleteRole(Role role, BaseResult result){
        if (role.getRoleId() == 0) {
            result.setMsg(ResultApi.deleteFailMsg);
            return 0;
        }
        int effectLine = roleMapper.deleteRole(role.getRoleId());
        if (effectLine > 0) {
            //删除角色关联表中roleId=role.getRoleId()的记录
            //int effectRp = rolePermissionMapper.deleteRolePermissionByRoleId(role.getRoleId());

            //将人员表中roleId=该Id记录的roleId更新为0
            //int effectPer = personnelMapper.deleteRoleId(role.getRoleId());

            result.setMsg(ResultApi.deleteSuccessMsg);
            return 1;
        }
        return 0;
    }

}
